/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_ttc7_oopen.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/11/24 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frhash2.h"
#include "frlog.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_stmt.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_ttc7_tti_oer.h"

/*******************************************************************************
*  FUNC     :  解析ttc7的close请求
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc7_oclose_unmarshal_req(Conntrack *p_st_conn
                                , L7session_ora *p_st_sess
                                , L7_ora_packet *p_st_ora_packet)
{
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    Sql_statement *p_st_stmt = NULL;
    short s_tmp = 0;
    int i_cursor = 0;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // seqNumber
        goto error;

    if (ora_mar_unmarshalSWORD(p_st_sess, p_st_ora_packet, &i_cursor) != FR_SUCC) // cursor_id
        goto error;

    if ((p_st_stmt = frdb_get_statement(p_st_session_private->p_hash_stmt, i_cursor)) != NULL)
    {
        // 把要关闭的游标增加到list中
        if (ora_add_closed_cursor(p_st_sess, (unsigned long) i_cursor) != FR_SUCC)
            goto error;

        // 设置l4层信息
        if (frdb_set_resultset_req(p_st_stmt, FRDB_LAST, 0, bytebuff_length(p_st_ora_packet->p_st_packet), &(p_st_ora_packet->st_timestamp_first), &(p_st_ora_packet->st_timestamp_last)) != FR_SUCC)
            goto error;
    }
    else
    {
        ORA_PRINT_EMERG("can not find stmt of cursor:[%d]\n", i_cursor);
        ORA_FRLOG_ERROR("can not find stmt of cursor:[%d]", i_cursor);
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  解析ttc7的close响应
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_hash_report                    (数据上报)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc7_oclose_unmarshal_resp(Conntrack *p_st_conn
                                , L7session_ora *p_st_sess
                                , L7_ora_packet *p_st_ora_packet
                                , Frhash_header *p_st_hash_report)
{

    short s_ret_code;                                // return code
    int i_flag = 0;

    while (i_flag == 0)
    {
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_ret_code) == FR_FAIL)
            goto error;

        switch (s_ret_code)
        {
            case 9:
                i_flag = 1;

//                ora_report_closed_stmt(p_st_conn, p_st_sess, p_st_ora_packet, p_st_hash_report);

                break;

            case 4:
                if (ora_ttc7_tti_oer_unmarshal(p_st_conn, p_st_sess, p_st_ora_packet, APM_ORA_SESS_IS_OCLOSE, NULL) == FR_FAIL)
                    goto error;
                i_flag = 1;
                break;

            default:
                goto error;
        }
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
